/*
 * Copyright (C) 2018 Tencent, Inc. All Rights Reserved.
 */
package com.szxb.facebus;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.widget.FrameLayout;

import com.tencent.deepsea.camera.CameraPreview;
import com.tencent.txccm.base.utils.LogUtil;
import com.tencent.txccm.base.utils.ToastShowHandler;
import com.tencent.wlxsdk.FaceResult;
import com.tencent.wlxsdk.WlxSdk;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;

/**
 * Created by johanlin on 2018/12/6.
 * 人脸demo页面
 */
public class FaceDemoActivity extends AppCompatActivity implements WlxSdk.FaceRecognitionCallBack {

    private static final int PERMISSIONS_REQUEST = 123;

    private static final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    private static final String PERMISSION_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    private CameraPreview mCameraView;
    private FrameLayout mContainer;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 这个初始化最好放到application里
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_demo);
        init();
    }

    private void init() {
        initUI();
        initData();
    }

    private void initData() {
        // 这里必须请求相机权限
        if (!hasPermission()) {
            requestPermission();
        } else {
            startFaceRecognition();
        }
//        startFaceRecognition();
    }

    private void startFaceRecognition() {
        // 先启动，再添加，因为要包含preview的初始化过程
        Lifecycle lifecycle = getLifecycle();
        mCameraView = new CameraPreview(this, null);
        WlxSdk.startFaceRecognition(mCameraView,lifecycle, this);
        mContainer.addView(mCameraView);
    }

    private void initUI() {
        mContainer = findViewById(R.id.texture);
    }

    @Override
    public void onRequestPermissionsResult(
            final int requestCode, final String[] permissions, final int[] grantResults) {
        if (requestCode == PERMISSIONS_REQUEST) {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                // 权限请求成功
                startFaceRecognition();
            } else {
                // 权限请求失败再次请求
                requestPermission();
            }
        }
    }

    /**
     * 相机和存储权限检查
     *
     * @return
     */
    private boolean hasPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return checkSelfPermission(PERMISSION_CAMERA) == PackageManager.PERMISSION_GRANTED &&
                    checkSelfPermission(PERMISSION_STORAGE) == PackageManager.PERMISSION_GRANTED;
        } else {
            return true;
        }
    }

    /**
     * 请求权限
     */
    private void requestPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (shouldShowRequestPermissionRationale(PERMISSION_CAMERA) ||
                    shouldShowRequestPermissionRationale(PERMISSION_STORAGE)) {
                ToastShowHandler.getInstance().showToast(this, "需要请求相机和存储权限");
            }
            requestPermissions(new String[] {PERMISSION_CAMERA, PERMISSION_STORAGE}, PERMISSIONS_REQUEST);
        }
    }

    @Override
    public void onCallBack(FaceResult result) {
        // TODO Result 里面返回用户名称和uid
        LogUtil.d("", "onCallBack() called with: result = [" + result + "]");
        // TODO 验证通过后黑名单处理逻辑
        switch (result.getCode()) {
            case SUCCESS:
                // 成功 处理
                if (isBlackUser(result.getUid())) {
                    // 黑名单用户处理逻辑
                } else {
                    // 正常用户 提示用户乘车
                }
                break;
            default:
                // 其他情况处理  待定
                break;
        }

    }

    private boolean isBlackUser(String uid) {
        // 根据当前用户的id 判断用户是否是黑名单用户
        return false;
    }
}
